/**
 * 相关资料页
 */
package com.xiaomuwu.android.ui.rele

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.Fragment
import com.xiaomuwu.android.R
import com.xiaomuwu.android.XiaoMuWuApplication
import com.xiaomuwu.android.ui.ForSearch
import kotlinx.android.synthetic.main.rele_res_fragment.*

class ReleResFragment:Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.rele_res_fragment,container,false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // 还原搜索框输入的内容
        searchReleResEdit?.setText(if(ForSearch.contentName==null){
            ""
        }else{
            ForSearch.contentName
        })
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        // 搜索框内容变化监听
        searchReleResEdit?.addTextChangedListener{ editable->
            // 更新搜索内容
            ForSearch.contentName=if(editable?.isNotBlank() == true) {
                editable.toString()
            }else {
                null
            }
            updateReleRes()
        }
    }

    override fun onPause() {
        super.onPause()
        ForSearch.contentName = null    // 重置搜索内容
    }

    /**
     * 刷新相关资料列表
     */
    private fun updateReleRes() {
        // 发送刷新相关资料列表广播
        val intent= Intent("com.xiaomuwu.android.UpdateReleRes")
        XiaoMuWuApplication.context.sendBroadcast(intent)
    }

}